layui.use(['form','jquery','table','layer'],function() {
    let form = layui.form,
        $ = layui.jquery,
        table =layui.table,
        layer = layui.layer;

    //加载数据表格
    let tabInstance = table.render({
        elem: '#lossList',
        url: '/customer-loss/search', //数据接口
        page: true, //开启分页
        // limit: 2, //每页显示的条数，默认10
        // limits: [2, 5, 10, 15,], //每页显示条数选择框数据
        // request: {
        //     pageName: 'current',
        //     limitName: 'size'
        // },
        cols: [[ //表头
            {field: 'id', title: '编号', width: 80, sort: true, align:"center"},
            {field: 'customerName', title: '客户',align:"center"},
            {field: 'customerMgr', title: '客户经理',align:"center"},
            {field: 'lastOrderTime', title: '上次下单时间',align:"center"},
            {field: 'lossTime', title: '确认流失时间',align:"center"},
            {field: 'status', title: '状态',templet:"#status",align:"center"},
            {title: '操作', width: 200, fixed: 'right', toolbar: "#lossListBar",align:"center"}
        ]]
    });

    //搜索监听启用
    form.on('submit(search)', function (data) {
        tabInstance.reload({
            where: data.field
        });
        return false;
    });

    function delay(suspend){
        let index = layui.layer.open({
            title :'暂缓流失',
            type : 2,
            content : 'views/khgl/delayLoss.jsp',
            btn:['保存', '确定流失', '帮助'],
            btnAlign: 'l',
            success : function(layero, index){
                let body = layer.getChildFrame('body', index);
                if(suspend){
                    body.find(".id").val(suspend.id);
                    body.find(".customerName").val(suspend.customerName);
                    body.find(".customerMgr").val(suspend.customerMgr);
                    body.find(".lastOrderTime").val(suspend.lastOrderTime);
                    body.find(".pauseWay").val(suspend.pauseWay);
                    body.find("#add_pause_way").val(suspend.addPauseWay);
                    form.render();
                }

                setTimeout(function(){
                    layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            },
            yes:function(index, layero){
                let body = layer.getChildFrame("body", index);
                let data = {};
                //把数组转为JSON
                body.find("#delay_loss").serializeArray().forEach(function(val){
                    data[val.name] = val.value;
                });
                console.log(data);
                $.post('customer-loss/save-delay',data,function(info){
                    layer.msg(info.msg);
                });
                layer.close(index);
                tabInstance.reload();
            }
        })
        layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layer.full(index);
        })
    }
    $(".help_btn").click(function(){
        delay();
    });

    function determin(confirm){
        let index = layui.layer.open({
            title :'暂缓流失',
            type : 2,
            content : 'views/khgl/confirmLoss.jsp',
            btn:['保存', '暂缓流失', '帮助'],
            btnAlign: 'l',
            success : function(layero, index){
                let body = layui.layer.getChildFrame('body', index);
                if(confirm){
                    body.find(".id").val(confirm.id);
                    body.find(".customerName").val(confirm.customerName);
                    body.find(".customerMgr").val(confirm.customerMgr);
                    body.find(".lastOrderTime").val(confirm.lastOrderTime);
                    body.find(".pauseWay").val(confirm.pauseWay);
                    body.find("#loss_reason").val(confirm.lossReason);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            },
            yes:function(index, layero) {
                let body = layer.getChildFrame("body", index);
                let data = {};
                //把数组转为JSON
                body.find("#delay_loss").serializeArray().forEach(function (val) {
                    data[val.name] = val.value;
                });
                console.log(data);
                $.post('customer-loss/save-confirm', data, function (info) {
                    layer.msg(info.msg);
                });
                layer.close(index);
                tabInstance.reload();
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }

    table.on('tool(lossList)', function(obj){
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值

        if(layEvent === 'suspend') {
            delay(data);
        }else if(layEvent === 'confirm'){
            determin(data);
        }
    });

})